package com.example.myapplication444

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class EditActivity : AppCompatActivity() {

    companion object {
        const val REQUEST_IMAGE_CAPTURE = 100
        const val EXTRA_AVATAR_BITMAP = "avatar_bitmap"
    }

    private lateinit var ivAvatar: ImageView
    private var newAvatarBitmap: Bitmap? = null   // 保存拍照结果

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit)
        ivAvatar = findViewById(R.id.ivAvatar)

        /* 1. 进入系统相机（缩略图） */
        findViewById<ImageButton>(R.id.btnTakePic).setOnClickListener {
            val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }

        /* 2. 接收原数据 */
        val name = intent.getStringExtra("name") ?: ""
        val studentId = intent.getStringExtra("studentId") ?: ""
        val phone = intent.getStringExtra("phone") ?: ""
        val email = intent.getStringExtra("email") ?: ""
        val gender = intent.getStringExtra("gender") ?: ""
        val hobby = intent.getStringExtra("hobby") ?: ""

        findViewById<EditText>(R.id.etName).setText(name)
        findViewById<EditText>(R.id.etStudentId).setText(studentId)
        findViewById<EditText>(R.id.etPhone).setText(phone)
        findViewById<EditText>(R.id.etEmail).setText(email)

        if (gender == "男") findViewById<RadioButton>(R.id.rbMale).isChecked = true
        else findViewById<RadioButton>(R.id.rbFemale).isChecked = true

        findViewById<CheckBox>(R.id.cbTravel).isChecked = hobby.contains("旅游")
        findViewById<CheckBox>(R.id.cbRead).isChecked  = hobby.contains("阅读")
        findViewById<CheckBox>(R.id.cbGame).isChecked  = hobby.contains("游戏")
        findViewById<CheckBox>(R.id.cbShop).isChecked  = hobby.contains("购物")


        findViewById<Button>(R.id.btnSave).setOnClickListener {
            val result = Intent().apply {
                putExtra("name",      findViewById<EditText>(R.id.etName).text.toString())
                putExtra("studentId", findViewById<EditText>(R.id.etStudentId).text.toString())
                putExtra("phone",     findViewById<EditText>(R.id.etPhone).text.toString())
                putExtra("email",     findViewById<EditText>(R.id.etEmail).text.toString())
                putExtra("gender",    if (findViewById<RadioButton>(R.id.rbMale).isChecked) "男" else "女")
                val hb = mutableListOf<String>()
                if (findViewById<CheckBox>(R.id.cbTravel).isChecked) hb.add("旅游")
                if (findViewById<CheckBox>(R.id.cbRead).isChecked)  hb.add("阅读")
                if (findViewById<CheckBox>(R.id.cbGame).isChecked)  hb.add("游戏")
                if (findViewById<CheckBox>(R.id.cbShop).isChecked)  hb.add("购物")
                putExtra("hobby", hb.joinToString("、"))
                // 关键：头像回传
                newAvatarBitmap?.let { putExtra(EXTRA_AVATAR_BITMAP, it) }
            }
            setResult(RESULT_OK, result)
            finish()
        }

        findViewById<Button>(R.id.btnCancel).setOnClickListener {
            setResult(RESULT_CANCELED)
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as? Bitmap
            imageBitmap?.let {
                ivAvatar.setImageBitmap(it)
                newAvatarBitmap = it   // 待回传
            }
        }
    }
}